<template>
  <div>
    <el-form
      :model="ruleForm"
      status-icon
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="DepartAdd-form"
    >
      <el-form-item label="部门" prop="department">
        <el-select v-model="ruleForm.department">
          <div v-for="ele in depart">
            <el-option :label="ele" :value="ele"></el-option>
          </div>
        </el-select>
      </el-form-item>
      <el-form-item label="职位" prop="position">
        <el-radio-group v-model="ruleForm.position" size="medium">
          <el-radio border label="普通员工"></el-radio>
          <el-radio border label="项目组长"></el-radio>
          <el-radio border label="产品经理"></el-radio>
          <el-radio border label="部门经理"></el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="薪资" prop="salary">
        <el-input
          type="text"
          v-model="ruleForm.salary"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >提交</el-button
        >
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { addSalary } from "../api/salary.js";
import { validSalaryMsg } from "../util/validate.js";
import { mapState } from "vuex";
export default {
  name: "Admin-depart-add",
  computed: {
    ...mapState(["depart"]),
  },
  methods: {
    async addInfos() {
      await addSalary(this.ruleForm);
    },
    submitForm() {
      this.$refs["ruleForm"].validate(async (valid) => {
        if (valid) {
          /* 判断是否重复添加 */
          if (
            validSalaryMsg(this.ruleForm.department, this.ruleForm.position)
          ) {
            swal("请勿重复添加!", "", "error");
            return;
          } else {
            this.addInfos();
            this.$message({
              message: "信息添加成功！",
              type: "success",
            });
          }
        } else {
          this.$message.error("信息填写不完整或格式不正确！");
          return false;
        }
      });
    },
    resetForm() {
      this.$refs["ruleForm"].resetFields();
    },
  },
  data() {
    return {
      ruleForm: {
        department: "",
        position: "",
        salary: "",
      },
      rules: {
        department: [
          { required: true, message: "请选择部门", trigger: "blur" },
        ],
        position: [{ required: true, message: "请选择职位", trigger: "blur" }],
        salary: [{ required: true, message: "薪资不能为空", trigger: "blur" }],
      },
    };
  },
};
</script>

<style scoped lang="less">
.DepartAdd-form {
  width: 400px;
  height: 200px;
}
.el-radio--medium.is-bordered {
  margin: 10px;
}
</style>